package com.itheima.web.listener;

import com.itheima.autoconfig.green.AliyunGreenTemplate;
import com.itheima.domain.db.Log;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.db.LogService;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class LogListener {
    //监听
    @DubboReference
    private LogService logService;

    @RabbitListener(queuesToDeclare = @Queue("tanhua.log"))
    public void listenerLog(Log log) {
        System.out.println("监听到了log--->" + log);
        logService.saveLog(log);
    }

    @DubboReference
    private MovementService movementService;

    @Autowired
    private AliyunGreenTemplate aliyunGreenTemplate;
    @RabbitListener(queuesToDeclare = @Queue("tanhua.movement.state"))
    public void listenerMovementState(String movementId) {
        System.out.println("监听到了movementId--->" + movementId);
        Movement movement = movementService.findById(new ObjectId(movementId));
        System.out.println("传过来的movement----"+movement);
        Boolean flag = aliyunGreenTemplate.checkText(movement.getTextContent());
        System.out.println("flag---"+flag);
        if (flag){
            movement.setState(1);
            System.out.println("修改的movement----"+movement);

            movementService.updateMovement(movement);
        }
    }
}
